[#include "/WEB-INF/pages/common/taglibs.ftl"]
[@c.url var="backUrl" value='/manage/Article_list.action' scope="page"]
	[@c.param name="categoryId" value="${categoryId}"/]
	[@c.param name="channelId" value="${channelId}"/]
	[@c.param name="currentPage" value="${currentPage}"/]
[/@c.url]
[#macro _script]
	<link rel="stylesheet" href="[@c.url value='/js/plugins/jquery-ztree/css/zTreeStyle/zTreeStyle.css'/]" type="text/css">
	<script type="text/javascript" src="[@c.url value='/js/custom/tables.js'/]"></script>
	<script type="text/javascript" src="[@c.url value='/js/plugins/jquery.alerts.js'/]"></script>
	<script type="text/javascript" src="[@c.url value='/js/plugins/jquery-ztree/js/jquery.ztree.core-3.1.js'/]"></script>
	<script type="text/javascript">
		var base = (function(){
			// 事件绑定
			function _addEventListeners() {
				// 维护
				$('a[act=edit]').unbind('click').click(function(){
					var id = $(this).parent().attr('tval');
					[@c.url var="editUrl" value='/manage/Article_edit.action' scope="page"]
						[@c.param name="categoryId" value="${categoryId}"/]
					[/@c.url]
					var url = "${editUrl}&id=" + id;
					//$.get(url, {'id':id, 'ajax':'true'}, function(data) {
					//	$('#body').html(data);
					//})
					window.location = url;
				})
				
				// 删除
				$('a[act=delete]').unbind('click').click(function(){
					var id = $(this).parent().attr('tval');
					var title = $('#delete-confirm').attr('title');
					var content = $('#delete-confirm').html();
					jConfirm(content, title, function(r) {
						if (r) {
							[@c.url	var="deleteUrl" value='/manage/Article_delete.action' scope="page"]
								[@c.param name="backUrl" value="${backUrl}"/]
								[@c.param name="id" value=""/]
							[/@c.url]
							window.location	= "${deleteUrl}" + id;
						}
					});
					return false;
				})
				
        		// 必须先渲染控件,后绑定事件
				//频道切换的select切换事件
				$('#channelSelect').change(function() {
					$('form[name=channelForm]').attr('action', "[@c.url value='/manage/Article_list.action'][/@c.url]");
					$('form[name=channelForm]').submit();
				})
				
				$('#articleSearch').unbind().submit(function(){
					var params = {'ajax':'true', 'piece':'true'};
					var url = $(this).attr('action');
					var paraArray = $(this).serializeArray();
					var index = 0;
					jQuery.each(paraArray, function(i, e) {
						if (e.value != ""){
							params[e.name] = e.value;
						}
					});
					$.get(url, params, function(result){
						$('#articleList').html(result);
						_addEventListeners();
					})
					return false;
				})
			}
			// 页面渲染
			function _treeRender () {
				[#--[@s.action name="Category_tree" executeResult="true" namespace="/manage"]
			    		[@s.param name="ajax" value="true" /]
						[@s.param name="channelId" value="channelId" /]
			    	[/@s.action];--]
		    	$.get("[@c.url value='/manage/Category_tree.action'/]", {ajax:true}, function(json){
		    	
			    	var zNodes = eval('(' + json + ')');
		            var setting = {
							data: {
								simpleData: {
									enable: true
								}
							},
							view: {
								// 渲染树
								addDiyDom: function (treeId, treeNode) {
									// 根节点以外的父节点
									//if (treeNode.pId != null) {
									//	$('#' + treeNode.tId).append("<a href='javascript:void(0);' onClick=\"return base.rmDir('" + treeNode.id + "')\">[删除]</a>");
									//}
									// 找到那个链接a
									var aObj = $("#" + treeNode.tId + "_a");
									if ($("#"+treeNode.id).length>0 || treeNode.pId == null) return;
									var editStr = "<span>&nbsp;&nbsp;&nbsp;&nbsp;</span><a href='javascript:void(0);' id='" + treeNode.tId +"_del' onClick=\"return base.rmDir('" + treeNode.id + "')\">[删除]</a>";
									aObj.append(editStr);
								}
							},
							callback: {
								onClick: function(event, treeId, treeNode, clickFlag){
									var isParent = treeNode.isParent;
									var id = treeNode.id;
									if (isParent) {
										//url = "[@c.url value='/manage/Resource_list.action'/]";
									} else {
										//url = "[@c.url value='/manage/Resource_view.action'/]";
									}
									$.get(base.config.url, {'categoryId':id, 'ajax':true, 'piece':true}, function(data){
					                	$('#articleTableList').html(data);
					                	_addEventListeners();
					                });
								}
							}
						};
					$.fn.zTree.init($("#categoryTree"), setting, zNodes);
		    	});
			}
			return {
                config: {
                	url : null,
                    theme: null
                },
                rmDir : function(id) {
	            	// 删除确认
					jConfirm("删除栏目后,子栏目及所有文章都会被删除,是否继续此操作?", "警告", function(r) {
						if (r) {
							$('<form id="rmCateForm" method="post" action="[@c.url value='/manage/Category_delete.action'/]"></form>').prependTo(document.body);
							$('#rmCateForm').append("<input type='hidden' name='id' value='" + id + "' /><input type='hidden' name='cascade' value='true'/>");
							$('#rmCateForm').submit();
						}
					});
				},
                init: function () {
                	// 初始化colorbox
					common_box.init();
					// 初始化checkbox
					common_checkbox.init();
                	_addEventListeners();
                },
                render : function() {
                	// 加载树数据
                	_treeRender();
                }
			}
		}());
		
		$(function(){
			var link = "[@c.url value='/manage/Article_list.action?channelId=${channelId}&currentPage=${currentPage}' /]";
			base.config.theme = getTheme();
			base.config.url = link;
            //Initializing the form
			base.init();
			base.render();
		})
	</script>
[/#macro]
[#if piece==true]
	<!--设置table渲染到指定ID节点-->
	[#assign renderTo="#articleTableList"]
	[#include "/WEB-INF/pages/manage/Article/_articleList.ftl"]
[#else]
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Insert title here</title>
		<meta name="menu" content="AdminMenu"/>
		<meta name="heading" content="heading"/>
		<!--for tab -->
		<meta name="form" content="current"/>
		
		[@_script /]
		<style type="text/css">
		</style>
	</head>
	<body id="body">
		<div class="contenttitle radiusbottom0" style="margin-bottom:1px;">
	    	<h2 id="vertical" class="form"><span>文章维护</span></h2>
	    </div><!--contenttitle-->
			<div class="wizard verwizard2" style="">
	        	<div class="verticalmenu widgetbox anchor" style="width:200px;">  
	        		<!--tree-->
	        		<div class="widgetcontent announcement">
	        			<ul id="categoryTree" class="ztree" style=""></ul>
	        		 </div>
			    </div>
			    [#assign renderTo="#articleTableList"]
				<div class="stepContainer" id="articleTableList">
					[#include "/WEB-INF/pages/manage/Article/_articleList.ftl"]
				</div>
				<div style="clear:both;"></div>
			</div>
	</body>
	</html>
[/#if]